using UnityEngine;
using System;
using System.Collections;

public class EnemyAI : MonoBehaviour
{
	public PathNode currentTarget;
	public PathNode nextTarget;
	public GameObject currentEnemy;
	public Vector3 speed;
	
	public BaseAI basAI;
	
	public PathNode[] recentNodes;
	private int nodeIndex = 0;
	
	public float searchDistance;
	
	
	void Awake () {
	//	currentTarget = GameObject.FindWithTag ("Player").transform.position;
		searchDistance = 5.0f;
		recentNodes = new PathNode[3];
	}
	
	// ref the base ai component
	void Start() {
		basAI = gameObject.GetComponent<BaseAI>();
		//gameObject.GetComponent<Mover>().customUpdateMoveFunction = basAI.customUpdateMoveFunction();
	}
	
	void startChasingTarget(GameObject target)
	{
		basAI.targetCharacter = target;
		basAI.thisState = BaseAI.CharState.Chasing;
	}
	
	void detectPathNode() {
		// if no target at first time, find the nearest target
		if (currentTarget == null) {
			PathNodeContainer pnc = GameObject.FindWithTag("PathNodes").GetComponent<PathNodeContainer>();
			PathNode[] pathNodes = pnc.GetComponentsInChildren<PathNode>();
			PathNode nearNode = null;
			float minDist = 99999.0f;
			
			foreach (PathNode p1 in pathNodes) {
				float tempDist = Vector3.Distance(gameObject.transform.position, p1.trans.position);
				if (tempDist < minDist) {
					minDist = tempDist;
					nearNode = p1;
				}
			}
			currentTarget = nearNode;
			basAI.changeDestination(nearNode.trans.position);
		}
		
		
		float dist = Vector3.Distance(gameObject.transform.position, currentTarget.trans.position);
		if (dist < searchDistance)
		{
			recentNodes[nodeIndex] = currentTarget;
			nodeIndex++;
			if (nodeIndex == 3) {
				nodeIndex = 0;
			}
			findNextNode();
		}
	}
	
	public void findNextNode() {
		ArrayList nodesToGo = new ArrayList();
		int count = currentTarget.realAdjacentNodes.Length;
		int count2 = recentNodes.Length;
		
		bool isRecent;
		// check all adjacent node with recently travalled nodes
		for (int i = 0; i < count; i++) {
			isRecent = false;
			for (int j = 0; j < count2; j++) {
				if (currentTarget.realAdjacentNodes[i] == recentNodes[j])
				{
					isRecent = true;
				}
			}
			
			// if not recently travalled, put to choice of next target
			if (!isRecent) {
				nodesToGo.Add((PathNode)currentTarget.realAdjacentNodes[i]);
				//nodesToGo.Add((PathNode)currentTarget.adjacentNodes[i]);
			}
		}
		UnityEngine.Random.seed = Mathf.FloorToInt(Time.time);
		// if all adjacent nodes are travalled(died road), treat as new start
		if (0 == nodesToGo.Count) {
			int nodeInd = Mathf.FloorToInt(UnityEngine.Random.Range(0.0f, currentTarget.realAdjacentNodes.Length));
			currentTarget = (PathNode)currentTarget.realAdjacentNodes[nodeInd];
		} else {
			int nodeInd = Mathf.FloorToInt(UnityEngine.Random.Range(0.0f, nodesToGo.Count));
			currentTarget = (PathNode)nodesToGo[nodeInd];
		}
		
		basAI.changeDestination(currentTarget.trans.position);
		
	}
	
	bool AiMove(MoverBase mb) {
		return true;	
	}
	
	void Update() {
		/*
		currentTarget = GameObject.FindWithTag ("Player").transform.position;
		float dist = Vector3.Distance(transform.position, target);
		if (dist > 1.7f)
		{
			Vector3 dir = currentTarget - transform.position;
			speed = (dir / dist) * 6.0f;
			transform.position += speed / 60.0f;
		}
		 */
		
		
		if (basAI.thisState == BaseAI.CharState.Searching) {
			detectPathNode();
		//	checkFacing(currentTarget.trans.position, cur
			//searchEnemy();
		}
		
	}
	
	// search enemy by finding the dot product
	void searchEnemy() {
		// NOTE : this method can find one people only at this time VVVVVVVVVV
		
		
		float dist = Vector3.Distance(gameObject.transform.position, 
		                              GameObject.FindWithTag("Player").transform.position);
		if (dist < searchDistance)
		{
			startChasingTarget(GameObject.FindWithTag("Player"));
		}
	}
}